package org.write.POI;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.joda.time.DateTime;
import org.junit.Test;

import java.io.FileOutputStream;
import java.io.IOException;

public class POI_Test {

    public static final String PATH = "C:\\Users\\chenhongkai\\Desktop\\Excel测试路径\\";

    //生成xls版本
    @Test
    public void testWrite03() throws IOException {

        Workbook workbook = new HSSFWorkbook();
        Sheet sheet = workbook.createSheet("获奖统计表");

        //第一行
        Row row1 = sheet.createRow(0);

        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("奖项");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("时间");

        //第二行
        Row row2 = sheet.createRow(1);

        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("优秀奖");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));


        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"奖惩表.xls");
        workbook.write(fileOutputStream);

        //关闭
        fileOutputStream.close();
    }

    //生成xlsx版本
    @Test
    public void testWrite07() throws IOException {

        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("获奖统计表");

        //第一行
        Row row1 = sheet.createRow(0);

        Cell cell11 = row1.createCell(0);
        cell11.setCellValue("奖项");
        Cell cell12 = row1.createCell(1);
        cell12.setCellValue("时间");

        //第二行
        Row row2 = sheet.createRow(1);

        Cell cell21 = row2.createCell(0);
        cell21.setCellValue("优秀奖");
        Cell cell22 = row2.createCell(1);
        cell22.setCellValue(new DateTime().toString("yyyy-MM-dd HH:mm:ss"));


        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"奖惩表.xlsx");
        workbook.write(fileOutputStream);

        //关闭
        fileOutputStream.close();
    }


    //生成excel--大量数据写入
    @Test
    public void testWriteMore() throws IOException {

        Workbook workbook = new SXSSFWorkbook();
        Sheet sheet = workbook.createSheet("获奖统计表");

        for(int r =0; r<1000; r++){
            Row row = sheet.createRow(r);
            for(int c=0; c<10; c++){
                Cell cell = row.createCell(c);
                cell.setCellValue(r+c);
            }
        }

        //生成一张表
        FileOutputStream fileOutputStream = new FileOutputStream(PATH+"大量数据.xlsx");
        workbook.write(fileOutputStream);

        //关闭
        fileOutputStream.close();
    }



}
